<template>
  <div class="add-msg">
    <el-form ref="addMsgFrom" :rules="rules" :model="addMsgFrom" :label-width="config.fromLabelWidth">
      <el-form-item :label="msgTitle.label" prop="msgTitle">
        <el-input v-model="addMsgFrom.msgTitle"></el-input>
      </el-form-item>
      <quill-editor ref="myTextEditor" :content="addMsgFrom.msgContent" :options = "editorOption" @change="onEditorChange($event)">
      </quill-editor>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('addMsgFrom')">创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import returnCode from '../../const/returnCode'
  export default {
    name: 'add-msg',
    data: function () {
      return {
        addMsgFrom: {
          msgTitle: '',
          msgContent: ''
        },
        rules: {
          msgTitle: [
            { required: true, message: '请输入标题', trigger: 'blur' }
          ]
        },
        msgTitle: {
          label: '标题'
        },
        msgContent: {
          label: '内容'
        },
        config: {
          rowGutter: 20
        },
        editorOption: {
          modules: {
            toolbar: [
              ['bold', 'italic', 'underline', 'strike'],
              ['blockquote', 'code-block'],
              [{ 'header': 1 }, { 'header': 2 }],
              [{'list': 'ordered'}, { 'list': 'bullet' }],
              [{'script': 'sub'}, { 'script': 'super' }],
              [{'indent': '-1'}, { 'indent': '+1' }],
              [{ 'direction': 'rtl' }],
              [{ 'size': ['small', false, 'large', 'huge'] }],
              [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
              [{ 'color': [] }, { 'background': [] }],
              [{ 'font': [] }],
              [{ 'align': [] }],
              ['clean']
            ]
          },
          placeholder: 'enter message info',
          theme: 'snow'
        }
      }
    },
    methods: {
      onEditorChange ({ editor, html, text }) {
        this.addMsgFrom.msgContent = html
      },
      onSubmit: function (formName) {
        let _this = this
        this.$refs[formName].validate(function (valid) {
          if (valid) {
            _this.$http.post('/msg/add', _this.addMsgFrom).then(res => {
              console.log(res)
              if (parseInt(res.data.status) === returnCode.success) {
                _this.$store.dispatch('notification/success', {vue: _this, message: '添加成功'})
                _this.$router.push({
                  path: '/ownMsg/msgList'
                })
              }
            }).catch(res => {

            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style>
  .ql-editor
  {
    height: 200px;
  }
  .addMsg
  {
    margin-top: 5px;
    text-align: left;
  }
  .box-card-body
  {
    padding: 15px;
  }
  .el-form-item__content
  {
    margin-top: 10px;
  }

</style>

